//
//  MPHttpTool.m
//  AT
//
//  Created by xiao6 on 2017/11/8.
//  Copyright © 2017年 ATLive. All rights reserved.
//

#import "MPHttpTool.h"
#import "UIDevice+ATKit.h"
#import <CoreTelephony/CoreTelephonyDefines.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

static NSString * const kMPHttpToolCacheDBTableName = @"kMPHttpToolCacheDBTableName";
NSString * const MPNoNetworkMsg = @"无网络连接，请检查网络设置";

@implementation MPHttpTool
+ (NSURLSessionDataTask *)POST:(NSString *)urlString
                        params:(id)params
                       success:(void (^)(NSURLSessionDataTask *task, ATApiResult *result))success
                       failure:(void (^)(NSString *errorMsg))failure {
    return [super POST:urlString params:params success:success failure:failure];
}

+ (NSURLSessionDataTask *)GET:(NSString *)urlString params:(id)params success:(void (^)(NSURLSessionDataTask *, ATApiResult *))success failure:(void (^)(NSString *))failure {
    return [super GET:urlString params:params success:success failure:failure];
}

+ (NSURLSessionDataTask *)manager:(AFHTTPSessionManager *)manager method:(ATHttpMethod)method urlString:(NSString *)urlString params:(id)params exParams:(NSDictionary *)exParams bodyBlock:(void (^)(id<AFMultipartFormData>))block progress:(void (^)(NSProgress *progress))progress completion:(void (^)(NSURLSessionDataTask *task, id responseObject, NSError *error))completion {

    manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:[ATOnline baseURL]]];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
    
    // 请求头
    // ...
    
    // 请求参数
    // ...
    
    // 请求地址
    // ...
    
    // 发起请求
    return [super manager:manager method:method urlString:urlString params:params exParams:exParams bodyBlock:block progress:progress completion:^(NSURLSessionDataTask *task, id responseObject, NSError *error) {
        ATApiResult *result = [ATApiResult modelWithJSON:responseObject];
        
        // 接口缓存
        // ...
        
        if (!result) {
            result = [[ATApiResult alloc] init];
            result.message = MPNoNetworkMsg;
            result.code = ATApiCodeServerNotFound;
        }
        result.requestUrl = task.originalRequest.URL.absoluteString;
        result.headParams = nil;
        result.params = params;
        
        NSLog(@"接口请求: \r\n%@", result);
        // 回调
        if (result.isSuccess) {
            !completion ?: completion(task, result, nil);
        }
        else {
            error = error ?: [NSError errorWithDomain:NSURLErrorDomain code:result.code userInfo:@{NSLocalizedFailureReasonErrorKey : SafeDicObj(result.message)}];
            !completion ?: completion(task, result, error);
        }
        
        // 登录过期处理
        
        // something others
    }];
}
@end
